const moment = require('moment')
const User = require('../../model/User')
const request = require('request')
const DouyinAccount = require('../../model/DouyinAccount')
const TargetLink = require('../../model/TargetLink')
const MessageTemplate = require('../../model/MessageTemplate')
const { sortedUniq } = require('lodash')




module.exports = {

    addMessageModel: async(req, res, next) => {
        try {
            let newMessage = new MessageTemplate({
                message : req.body.message,
                useTimes : 0,
                updatetime : new Date(),
                isDelete: 0,
            })
            await newMessage.save()
            res.ok('获取成功', newMessage)
        } catch (e) {
            next(e)
        }
    },
    updateMessageModel: async(req, res, next) => {
        try {
            let messageTemplate = await MessageTemplate.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            messageTemplate.message = req.body.message
            messageTemplate.updatetime = new Date()
            await messageTemplate.save()
            res.ok('修改成功', messageTemplate)
        } catch (e) {
            next(e)
        }
    },
    getMessageModelList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            let count = await MessageTemplate.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await MessageTemplate.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },
    deleteMessageModel: async(req, res, next) => {
        try {
            let messageTemplate = await MessageTemplate.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            messageTemplate.isDelete=1
            await messageTemplate.save()
            res.ok('删除成功', messageTemplate)
        } catch (e) {
            next(e)
        }
    },
    getOneDouyinAccount: async(req, res, next) => {
        try {
            let douyinAccount = await DouyinAccount.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            res.ok('成功', douyinAccount)
        } catch (e) {
            next(e)
        }
    },
    addDouyinAccount: async(req, res, next) => {
        try {
            let newDouyinAccount = new DouyinAccount({
                name : req.body.name,
                cellphone : req.body.cellphone,
                updatetime : new Date(),
                isDelete: 0,
            })
            if(req.body.password){
                newDouyinAccount.password = req.body.password
            }
            await newDouyinAccount.save()
            res.ok('获取成功', newDouyinAccount)
        } catch (e) {
            next(e)
        }
    },
    updateDouyinAccount: async(req, res, next) => {
        try {
            let douyinAccount = await DouyinAccount.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            douyinAccount.name = req.body.name
            douyinAccount.cellphone = req.body.cellphone
            if(req.body.password){
                douyinAccount.password = req.body.password
            }
            douyinAccount.updatetime = new Date()
            await douyinAccount.save()
            res.ok('修改成功', douyinAccount)
        } catch (e) {
            next(e)
        }
    },
    getDouyinAccountList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            let count = await DouyinAccount.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await DouyinAccount.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },
    deleteDouyinAccount: async(req, res, next) => {
        try {
            let douyinAccount = await DouyinAccount.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            douyinAccount.isDelete=1
            await douyinAccount.save()
            res.ok('删除成功', douyinAccount)
        } catch (e) {
            next(e)
        }
    },
    getOneTargetLink: async(req, res, next) => {
        try {
            let targetLink = await TargetLink.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            res.ok('成功', targetLink)
        } catch (e) {
            next(e)
        }
    },
    addTargetLink: async(req, res, next) => {
        try {
            let newTargetLink = new TargetLink({
                forwardStr : req.body.forwardStr,
                // url : String,
                updatetime : new Date(),
                isDelete: 0,
            })
            await newTargetLink.save()
            res.ok('获取成功', newTargetLink)
        } catch (e) {
            next(e)
        }
    },
    updateTargetLink: async(req, res, next) => {
        try {
            let targetLink = await TargetLink.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            targetLink.forwardStr = req.body.forwardStr
            // targetLink.url = req.body.url
            targetLink.updatetime = new Date()
            await targetLink.save()
            res.ok('修改成功', targetLink)
        } catch (e) {
            next(e)
        }
    },
    getTargetLinkList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            let count = await TargetLink.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await TargetLink.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },
    deleteTargetLink: async(req, res, next) => {
        try {
            let targetLink = await TargetLink.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            targetLink.isDelete=1
            await targetLink.save()
            res.ok('删除成功', targetLink)
        } catch (e) {
            next(e)
        }
    },
};